package com.example.demo;

import com.aspose.words.*;
import com.aspose.words.Shape;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

import java.awt.*;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Properties;

public class DocToPdf {

    private static final String SUCCESS = "SUCCESS";
    private static final String ERROR ="ERROR";

    public static void main(String[] args) {
//        doc2pdf("C:\\tempFiles\\test2003.doc","C:\\tempFiles\\test2003.pdf");
        doc2pdf("C:\\tempFiles\\第一课：zookeeper 特性与节点说明.docx","C:\\tempFiles\\test2007.pdf");
//        doc2pdf("C:\\tempFiles\\test.txt","C:\\tempFiles\\test.pdf");

    }

    public static String doc2pdf(String docPath, String pdfPath) {
        // 验证License 若不验证则转化出的pdf文档会有水印产生
        boolean license = ExcelToPdf.getLicense();
        System.out.println("license-->"+license);

        try {
            Document document = new Document(docPath);
            System.out.println("pdfPath = "+pdfPath);
            File pdfFile = new File(pdfPath);
            //判断是否windows系统，Linux要读取字体，否则pdf字体为⽅格
            Properties prop = System.getProperties();
            String os = prop.getProperty("os.name");
            if (os != null && os.toLowerCase().indexOf("linux") > -1) {
                System.out.println("linux加载字体");
                FontSettings.setFontsFolder(File.separator + "usr" + File.separator + "share" + File.separator + "fonts" +File.separator + "Fonts", true);
                System.out.println("linux加载字体完成");
            }

            FileOutputStream fileOutputStream = new FileOutputStream(pdfFile);
            System.out.println("pdf文件："+fileOutputStream);
            insertWatermarkText(document, "四叶草的诗雨");
            document.save(fileOutputStream, SaveFormat.PDF);

            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ERROR;
        }
    }


    private static void insertWatermarkText(Document doc, String watermarkText) throws Exception
    {
        Shape watermark = new Shape(doc, ShapeType.TEXT_PLAIN_TEXT);
        //水印内容
        watermark.getTextPath().setText(watermarkText);
        //水印字体
        watermark.getTextPath().setFontFamily("宋体");
        //水印宽度
        watermark.setWidth(500);
        //水印高度
        watermark.setHeight(100);
        //旋转水印
        watermark.setRotation(-40);
        //水印颜色
        watermark.getFill().setColor(Color.lightGray);
        watermark.setStrokeColor(Color.lightGray);
        watermark.setRelativeHorizontalPosition(RelativeHorizontalPosition.PAGE);
        watermark.setRelativeVerticalPosition(RelativeVerticalPosition.PAGE);
        watermark.setWrapType(WrapType.NONE);
        watermark.setVerticalAlignment(VerticalAlignment.CENTER);
        watermark.setHorizontalAlignment(HorizontalAlignment.CENTER);
        Paragraph watermarkPara = new Paragraph(doc);
        watermarkPara.appendChild(watermark);
        for (Section sect : doc.getSections())
        {
            insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_PRIMARY);
            insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_FIRST);
            insertWatermarkIntoHeader(watermarkPara, sect, HeaderFooterType.HEADER_EVEN);
        }
        System.out.println("Watermark Set");
    }
    private static void insertWatermarkIntoHeader(Paragraph watermarkPara, Section sect, int headerType) throws Exception
    {
        HeaderFooter header = sect.getHeadersFooters().getByHeaderFooterType(headerType);
        if (header == null)
        {
            header = new HeaderFooter(sect.getDocument(), headerType);
            sect.getHeadersFooters().add(header);
        }
        header.appendChild(watermarkPara.deepClone(true));
    }


}
